package com.machine.filling.db.dao;


import android.os.Environment;

import com.machine.filling.MyApp;
import com.machine.filling.db.greendao.AdModleDao;
import com.machine.filling.db.modle.AdModle;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/6/6 0006.
 */

public class AdDao {

    private static AdDao instance;
    private static AdModleDao adModleDao;
    private static AdModle adModle;

    private AdDao() {
    }

    public static AdDao getInstance() {
        if (instance == null) {
            instance = new AdDao();
            adModleDao = MyApp.getInstance().getDaoSession().getAdModleDao();
            adModle = new AdModle();
        }
        return instance;
    }

    public void saveAd(AdModle modle) {
        adModleDao.insert(modle);
    }

    public void saveAdList(List<AdModle> adModles){
        for (AdModle modle :adModles) {
            adModleDao.insert(modle);
        }
    }

    public void update(AdModle modle) {

        AdModle ad = adModleDao.queryBuilder()
                .where(AdModleDao.Properties.Id.eq(modle.getId()))
                .unique();
        if (ad != null) {
            adModleDao.update(modle);
        } else {
            saveAd(modle);
        }
    }

    public List<AdModle> getAll(){
        return adModleDao.queryBuilder().list();
    }

    public AdModle getAdByHttpImgPath(String httpImgPath){
        AdModle ad = adModleDao.queryBuilder().where(AdModleDao.Properties.HttpImgPath.eq(httpImgPath)).unique();
        if (ad != null) {
            return ad;
        } else {
            return new AdModle();
        }
    }

    public void deleteAD(AdModle ad){
        adModleDao.delete(ad);
    }

    public void deleteAll(){
        adModleDao.deleteAll();
    }
}
